package com.westvalley.common.kit;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * 字符串
 * @author MS
 *
 */
public class StringKit {
	
	public static final int INDEX_NOT_FOUND = -1;
	public static final String EMPTY = "";
	
	public static String removedFirstTailStr(String str,String c){
		if( str != null ){
			str = str.trim();
			if(str.startsWith(c)) str = str.substring(c.length());
			if(str.endsWith(c)) str = str.substring(0, str.length()-c.length());
		}
		return str;
	}
	
	
	/**
	 * 首字母变小写
	 */
	public static String firstCharToLowerCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'A' && firstChar <= 'Z') {
			char[] arr = str.toCharArray();
			arr[0] += ('a' - 'A');
			return new String(arr);
		}
		return str;
	}
	
	/**
	 * 首字母变大写
	 */
	public static String firstCharToUpperCase(String str) {
		char firstChar = str.charAt(0);
		if (firstChar >= 'a' && firstChar <= 'z') {
			char[] arr = str.toCharArray();
			arr[0] -= ('a' - 'A');
			return new String(arr);
		}
		return str;
	}
	
	/**
	 * 字符串为 null 或者为  "" 时返回 true
	 */
	public static boolean isBlank(String str) {
		return str == null || "".equals(str.trim()) ? true : false;
	}
	
	/**
	 * 字符串不为 null 而且不为  "" 时返回 true
	 */
	public static boolean notBlank(String str) {
		return str == null || "".equals(str.trim()) ? false : true;
	}
	
	
	public static String list2String(List<String> list,String strDelimiter,boolean isQuotes){
		if(list==null)
			return "";
		return list2String(list.toArray(new String[0]),strDelimiter,isQuotes);
	}
	
	public static String set2String(Set<String> set,String strDelimiter,boolean isQuotes){
		return list2String(set.toArray(new String[0]),strDelimiter,isQuotes);
	}
	
	/**
	 * String[]转换为String
	 * @param list	
	 * @param strDelimiter 分隔符
	 * @param isQuotes	是否加单引号
	 * @return
	 */
	public static String list2String(String[] list,String strDelimiter,boolean isQuotes){
		String strList = "";
		for(String str : list){
			if(isQuotes)
				strList += strDelimiter+"'"+str+"'";
			else
				strList += strDelimiter+str;
		}
		if(strList.length()>1)
			strList = strList.substring(1,strList.length());
		return strList;
	}
	
	public static boolean notBlank(String... strings) {
		if (strings == null)
			return false;
		for (String str : strings)
			if (str == null || "".equals(str.trim()))
				return false;
		return true;
	}
	
	public static boolean notNull(Object... paras) {
		if (paras == null)
			return false;
		for (Object obj : paras)
			if (obj == null)
				return false;
		return true;
	}
	
	public static String null2Str(String str){
		return trim(str);
	}
	
	public static String null2Str(String str,String str2){
		if("".equals(null2Str(str)))
			str = str2;
		return trim(str);
	}
	
	public static String null2o(Object o){
		if(o==null)
			return "";
		else
			return o.toString();
	}
	
	public static String trim(String str){
		if(str!=null) return str.trim();
		return "";
	}
	
	public static String date2Str(Date date,String format){
		if(date != null){
			SimpleDateFormat sdf = new SimpleDateFormat(format);  
			return sdf.format(date);
		}
		return null;
	}
	
	public static String strFillChar(String str,int length,char c){
		if(str!=null){
			if(str.length()<length){
				int cs = (length-str.length());
				for(int i=0;i<cs;i++){
					str += c;
				}
				return str;
			}else{
				return str.substring(0,length);
			}
		}
		return null;
	}
	
	public static List<String> strCutting(String str,String c){
		List<String> list = new ArrayList<String>();
		if( notBlank(str) ){
			for( String s : str.split(c) ){
				list.add(s);
			}
		}
		return list;
	}
	
	public static String getCurrentDateTime(){
		return date2Str(new Date(), "yyyy-MM-dd HH:mm:ss");
	}
	
	public static String getCurrentDate(){
		return date2Str(new Date(), "yyyy-MM-dd");
	}
	
	public static String toUpperCase(String str){
		return null2Str(str).toUpperCase();
	}
	
	public static String toLowerCase(String str){
		return null2Str(str).toLowerCase();
	}
	
	public static boolean endsWith(String str,String with){
		return toUpperCase(str).endsWith(null2Str(with).toUpperCase());
	}
	
	public static boolean endsWith(String str,String... withs){
		for(String with : withs){
			if(!endsWith(str,with))
				return false;
		}
		return true;
	}
	
	public static boolean equals(String str1,String str2){
		return null2Str(str1).equals(str2);
	}
	
	public static boolean equals(String str1,String... str2){
		for(String str : str2){
			if(equals(str1,str))
				return true;
		}
		return false;
	}
	
	public static boolean equalsIgnoreCase(String str1,String str2){
		return null2Str(str1).equalsIgnoreCase(str2);
	}
	
	public static boolean equalsIgnoreCase(String str1,String... str2){
		for(String str : str2){
			if(equalsIgnoreCase(str1,str))
				return true;
		}
		return false;
	}
	
	public static String replace(String str,String oldChar,String newChar){
		return trim(str).replace(oldChar, newChar);
	}
	
	public static String substring(String str,int beginIndex){
		str = trim(str);
		return str.length()>beginIndex?str.substring(beginIndex):str;
	}
	
	public static String[] split(String str,String regex){
		return trim(str).split(regex);
	}
	
	public static int indexOf(String str,String ch){
		return trim(str).indexOf(ch);
	}
	
	public static int lastIndexOf(String str,String ch){
		return trim(str).lastIndexOf(ch);
	}
	
	public static boolean isInteger(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");  
	    return pattern.matcher(str).matches();     
	}
	
	public static void main(String[] args) {
	}
}




